package com.imyuanma.qingyun.lowcode.controller;

import com.imyuanma.qingyun.common.model.request.WebRequest;
import com.imyuanma.qingyun.common.model.response.Result;
import com.imyuanma.qingyun.common.util.AssertUtil;
import com.imyuanma.qingyun.common.util.CollectionUtil;
import com.imyuanma.qingyun.interfaces.common.model.vo.KeyValueVO;
import com.imyuanma.qingyun.interfaces.monitor.annotation.Trace;
import com.imyuanma.qingyun.lowcode.model.data.LcpDbColumnDO;
import com.imyuanma.qingyun.lowcode.model.data.LcpDbTableDO;
import com.imyuanma.qingyun.lowcode.model.enums.ELcpButtonCategoryEnum;
import com.imyuanma.qingyun.lowcode.model.enums.ELcpColumnTypeEnum;
import com.imyuanma.qingyun.lowcode.model.vo.LcpDbColumnVO;
import com.imyuanma.qingyun.lowcode.model.vo.LcpDbTableDetailVO;
import com.imyuanma.qingyun.lowcode.service.ILcpDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据库web
 *
 * @author wangjy
 * @date 2022/05/04 15:36:29
 */
@RestController
@RequestMapping(value = "/lowcode/db", method = {RequestMethod.POST})
public class LcpDbController {
    /**
     * 数据库服务
     */
    @Autowired
    private ILcpDbService dbService;

    @Trace("查询支持的数据库字段类型")
    @RequestMapping(value = "/getDbColumnTypeList")
    public Result<List<KeyValueVO>> getDbColumnTypeList() {
        return Result.success(Arrays.stream(ELcpColumnTypeEnum.values()).map(item -> new KeyValueVO(item.getType(), item.getText())).collect(Collectors.toList()));
    }

    /**
     * 查询数据库表列表
     * @return
     */
    @Trace("查询数据库表列表")
    @RequestMapping("/getTableList")
    public Result<List<LcpDbTableDO>> getTableList() {
        return Result.success(dbService.getTableList());
    }

    /**
     * 查询数据库表详情
     * @param webRequest
     * @return
     */
    @Trace("查询数据库表详情")
    @RequestMapping("/getTableDetail")
    public Result<LcpDbTableDetailVO> getTableDetail(@RequestBody WebRequest<String> webRequest) {
        AssertUtil.notNull(webRequest);
        AssertUtil.notBlank(webRequest.getBody());
        // 表信息
        LcpDbTableDO tableDO = dbService.getTableByTableName(webRequest.getBody());
        if (tableDO == null) {
            return Result.error(String.format("表[%s]不存在", webRequest.getBody()));
        }

        // 表字段
        List<LcpDbColumnDO> columns = dbService.getColumnListByTableName(webRequest.getBody());
        AssertUtil.notEmpty(columns, "表字段为空");

        // todo 表索引

        // 返回结果
        return Result.success(new LcpDbTableDetailVO(tableDO, columns, null));
    }

    @Trace("修改数据库字段")
    @RequestMapping(value = "/updateDbColumn")
    public Result updateDbColumn(@RequestBody WebRequest<LcpDbColumnVO> webRequest) {
        AssertUtil.notNull(webRequest);
        AssertUtil.notNull(webRequest.getBody());
        dbService.modifyDbColumn(webRequest.getBody());
        return Result.success();
    }

    @Trace("新增数据库字段")
    @RequestMapping(value = "/addDbColumn")
    public Result addDbColumn(@RequestBody WebRequest<LcpDbColumnVO> webRequest) {
        AssertUtil.notNull(webRequest);
        AssertUtil.notNull(webRequest.getBody());
        dbService.addDbColumn(webRequest.getBody());
        return Result.success();
    }

    @Trace("创建数据库表")
    @RequestMapping(value = "/createTable")
    public Result createTable(@RequestBody WebRequest<LcpDbTableDetailVO> webRequest) {
        AssertUtil.notNull(webRequest);
        AssertUtil.notNull(webRequest.getBody());
        dbService.createTable(webRequest.getBody());
        return Result.success();
    }
}
